package com.back.controller;

import com.back.common.utils.ResultBean;
import com.back.dto.JobCategoryDTO;
import com.back.entity.JobCategory;
import com.back.service.IJobCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 职业分类表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-02
 */
@RestController
@RequestMapping("/job-category")
@RequiredArgsConstructor
public class JobCategoryController {

    private final IJobCategoryService jobCategoryService;

    // mybatis-plus 分页查询
    @PostMapping("/page")
    public ResultBean<IPage<JobCategory>> page(@RequestBody JobCategoryDTO jobCategoryDTO) {
        IPage<JobCategory> page = new Page<>(jobCategoryDTO.getCurrent(), jobCategoryDTO.getSize());
        LambdaQueryWrapper<JobCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(JobCategory::getCreateTime);
        IPage<JobCategory> jobCategoryPage = jobCategoryService.page(page, queryWrapper);
        return ResultBean.ok(jobCategoryPage);
    }
    
    @GetMapping("/list")
    public ResultBean<List<JobCategory>> list() {
        LambdaQueryWrapper<JobCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(JobCategory::getCreateTime);
        List<JobCategory> jobCategoryList = jobCategoryService.list(queryWrapper);
        return ResultBean.ok(jobCategoryList);
    }

    @PostMapping("/add")
    public ResultBean<String> add(@RequestBody JobCategory jobCategory) {
        jobCategory.setCreateTime(LocalDateTime.now());
        jobCategory.setUpdateTime(LocalDateTime.now());
        jobCategoryService.save(jobCategory);
        return ResultBean.ok("增加成功！");
    }

    @PostMapping("/update")
    public ResultBean<String> update(@RequestBody JobCategory jobCategory) {
        jobCategory.setUpdateTime(LocalDateTime.now());
        jobCategoryService.updateById(jobCategory);
        return ResultBean.ok("修改成功！");
    }

    @GetMapping("/delete/{id}")
    public ResultBean<String> delete(@PathVariable Long id) {
        jobCategoryService.removeById(id);
        return ResultBean.ok("删除成功！");
    }
}
